/**
 * 
 */
package org.t2.backstage.module.giftcode.service;

import java.util.ArrayList;
import java.util.List;

import org.banish.sql.core.util.QuerySet;
import org.gaming.backstage.PageData;
import org.gaming.ruler.lifecycle.Lifecycle;
import org.gaming.service.service.AbstractService;
import org.gaming.tool.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.t2.backstage.module.giftcode.model.GiftCode;

import sh.t2.share.module.mail.MailAttachment;


/**
 * @author YY
 *
 */
@Service
public class GiftCodeService extends AbstractService<GiftCode> implements Lifecycle {
	
	@Autowired
	private GiftCodeRandomLogService giftCodeRandomLogService;
	
	public boolean updateUnuse(GiftCode giftCode) {
		return this.repository().getBaseDao().updateWhen(giftCode, "player_id = 0");
	}

	@Override
	public void start() throws Exception {
		long count = this.repository().getBaseDao().countWhere("");
		if(count > 0) {
			return;
		}
		
		List<GiftCode> codeList = new ArrayList<>();
		List<MailAttachment> rewards = new ArrayList<>();
		MailAttachment reward1 = new MailAttachment();
		reward1.setItem(101001);
		reward1.setNum(1);
		MailAttachment reward2 = new MailAttachment();
		reward2.setItem(101002);
		reward2.setNum(1);
		MailAttachment reward3 = new MailAttachment();
		reward3.setItem(102001);
		reward3.setNum(1);
		rewards.add(reward1);
		rewards.add(reward2);
		rewards.add(reward3);
		
		for(int i = 0; i < 100; i++) {
			GiftCode codeShare = new GiftCode();
			codeShare.setCode("vip" + i);
			codeShare.setName("测试" + i);
			codeShare.setType(1);
			codeShare.setNum(1);
			codeShare.setUserId(1);
			codeShare.setUserName("root");
			codeShare.setExpireTime(System.currentTimeMillis() + DateTimeUtil.ONE_YEAR_MILLIS);
			codeShare.setRewards(rewards);
			codeShare.setChannelId(0);
			codeShare.setServerUid(0);
			codeShare.setUsable(true);
			codeShare.setUseUserId(1);
			codeShare.setUseUserName("root");
			codeList.add(codeShare);
		}
		
		GiftCode codeRandom = new GiftCode();
		codeRandom.setCode("随机生成");
		codeRandom.setName("测试随机生成");
		codeRandom.setType(2);
		codeRandom.setNum(100);
		codeRandom.setExpireTime(System.currentTimeMillis() + DateTimeUtil.ONE_YEAR_MILLIS);
		codeRandom.setRewards(rewards);
		codeRandom.setChannelId(0);
		codeRandom.setServerUid(0);
		codeRandom.setUsable(true);
		codeRandom.setUserId(1);
		codeRandom.setUserName("root");
		codeRandom.setUseUserId(1);
		codeRandom.setUseUserName("root");
		codeList.add(codeRandom);
		
		this.repository().insertAll(codeList);
		
		giftCodeRandomLogService.trySpawnCodes(codeRandom, 8);
	}

	@Override
	public void mergeServer(int mergeDbZoneId) {
		//不用实现
	}
	
	public PageData<GiftCode> query(QuerySet querySet) {
		querySet.formWhere();
		List<GiftCode> result = this.repository().getBaseDao().queryListWhere(querySet.getWhere(), querySet.getParams());
		long count = repository().getBaseDao().countWhere(querySet.getCountWhere(), querySet.getCountParams());
		PageData<GiftCode> queryPage = new PageData<>(count, result);
		return queryPage;
	}
	
	public GiftCode getEntity(long id) {
		return this.repository().getByKeys(id);
	}
}
